package com.hfzy.ihk.web.websocket.support.toBrowser.handler;

import com.hfzy.ihk.web.websocket.support.WebSocketManager;
import com.hfzy.ihk.web.websocket.support.base.AbstractBrowserWebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;

/**
 * Created by Administrator on 2016/11/21.
 *
 * @author wws
 *
 * 暂用于弹屏
 */
public class ChromePluginWebSocketHandler extends AbstractBrowserWebSocketHandler {

    private static final Logger logger = LoggerFactory.getLogger(ChromePluginWebSocketHandler.class);

    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {

        Map<String,Object> attributes = webSocketSession.getAttributes();

        String zyUserId = (String)attributes.get("userId");

        WebSocketManager.browserUserWebSocketSession4ChromePluginMap.put(zyUserId, webSocketSession);

        logger.info("webSocket has connected,userId:{}",zyUserId);

    }

    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {

        Map<String,Object> attributes = webSocketSession.getAttributes();

        String userId = (String)attributes.get("userId");
        String token = (String)attributes.get("token");
        String machineId = (String)attributes.get("machineId");

        logger.info("afterConnectionClosed: userId:{},machineId:{},token:{}, close reason:{}",userId,machineId,token,closeStatus.getReason());

        WebSocketSession bs = WebSocketManager.browserUserWebSocketSession4ChromePluginMap.get(userId);

        if(webSocketSession == bs) {
            WebSocketManager.browserUserWebSocketSession4ChromePluginMap.remove(userId);
        }
    }

}
